package com.krain.meijusky.util;

import android.app.Activity;
import android.app.ActivityOptions;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;


import com.krain.corelibrary.util.UIUtil;
import com.krain.corelibrary.widget.MaterialDialog;
import com.krain.meijusky.R;
import com.krain.meijusky.activity.BaseActivity;

import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.UiThread;

import java.util.List;

/**
 * Created by yamin on 2015/7/31.
 * BaseActivity 和 BaseFragment的公用方法工具类
 */
@EBean(scope = EBean.Scope.Singleton)
public class BaseUtil {

    private static AlertDialog networkDialog;
    private MaterialDialog alertDialog;
    Snackbar snackbar;


    @UiThread(propagation = UiThread.Propagation.REUSE)
    public void startActivityOnAnimation(Activity context, Intent intent) {
        View contentView = UIUtil.getFirstChildView(context);
        if (contentView != null && contentView.getVisibility() != View.GONE) {
            ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(context);
            ActivityCompat.startActivity(context, intent, options.toBundle());
        } else {
            context.startActivity(intent);
        }
    }

    @UiThread(propagation = UiThread.Propagation.REUSE)
    public void startActivityOnAnimation(Activity context, Intent intent, int requestCode) {
        View contentView = UIUtil.getFirstChildView(context);
        ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(context);
        ActivityCompat.startActivityForResult(context, intent, requestCode, options.toBundle());
    }

    @UiThread(propagation = UiThread.Propagation.REUSE)
    public void showSnackBar(Activity context, String content, String actionText, View.OnClickListener onClickListener) {
        View contentView = UIUtil.getFirstChildView(context);
        snackbar = Snackbar.make(contentView, content, Snackbar.LENGTH_LONG);
        if (null == actionText) {
            snackbar.show();
        } else {
            snackbar.setAction(actionText, onClickListener);
            snackbar.show();
        }

    }

    @UiThread(propagation = UiThread.Propagation.REUSE)
    public void showSnackBar(Activity context, String content) {
        View contentView = UIUtil.getFirstChildView(context);
        snackbar = Snackbar.make(contentView, content, Snackbar.LENGTH_LONG);
        snackbar.show();
    }

    public Snackbar getSnackbar() {
        return snackbar;
    }

    /**
     * 关闭键盘的方法
     *
     * @author LinKen
     */
    public void closeKeyboard(Activity con) {
        View view = con.getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) con
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    /**
     * 关闭键盘的方法
     *
     * @author LinKen
     */
    public void showKeyborad(Activity con) {
        View view = con.getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) con
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(UIUtil.getFirstChildView(con), InputMethodManager.SHOW_FORCED);
        }
    }


    @UiThread(propagation = UiThread.Propagation.REUSE)
    public void showNetworkErrorDialog(final Activity activity) {
        if (null == networkDialog) {
            networkDialog = new AlertDialog.Builder(activity).setTitle(R.string.error_warning)
                    .setMessage("网络发生异常，请检查网络设置？").setPositiveButton("设置网络", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (null != dialog) {
                                dialog.cancel();
                                networkDialog = null;
                            }
                            if (android.os.Build.VERSION.SDK_INT > 10) {
                                // 3.0以上打开设置界面，也可以直接用ACTION_WIRELESS_SETTINGS打开到wifi界面
                                activity.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
                            } else {
                                activity.startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
                            }
                        }
                    }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (null != dialog) {
                                dialog.cancel();
                                networkDialog = null;
                            }
                        }
                    }).create();
            networkDialog.show();
        } else if (!networkDialog.isShowing())
            networkDialog.show();
    }


    /**
     * 显示提示dialog
     *
     * @param title
     * @param warningMsg 提示信息
     */
    @UiThread(propagation = UiThread.Propagation.REUSE)
    public void showWarningDialog(Context context, String title, String warningMsg, View.OnClickListener onOkBtnClickListener) {
        if (context instanceof Activity && ((Activity) context).isFinishing()) {
            return;
        }
        try {
            if (null != alertDialog) {
                if (alertDialog.getmMsg().getText().toString().equals(warningMsg) && alertDialog.isShowing())
                    return;
                if (context != null && context instanceof Activity && !((Activity) context).isFinishing())
                    alertDialog.dismiss();
                alertDialog = null;
            }

            alertDialog = new MaterialDialog(context);
            alertDialog.setTitle(title);
            alertDialog.setMessage(warningMsg);
            alertDialog.setOnComfirmClick(onOkBtnClickListener);
            alertDialog.setCancelGone();
            alertDialog.setmCanDismiss(false);
            alertDialog.show();
        } catch (Exception e) {
        }
    }


    /**
     * 显示选择性的dialog
     *
     * @param title
     * @param warningMsg 提示信息
     */
    @UiThread(propagation = UiThread.Propagation.REUSE)
    public void showDialog(Context context, String title, String warningMsg, View.OnClickListener onOkBtnClickListener,
                           View.OnClickListener onCancelBtnClickListener) {
        if (context instanceof Activity && ((Activity) context).isFinishing()) {
            return;
        }
        try {
            if (null != alertDialog) {
                if (alertDialog.getmMsg().getText().toString().equals(warningMsg) && alertDialog.isShowing())
                    return;
                alertDialog.dismiss();
                alertDialog = null;
            }
            alertDialog = new MaterialDialog(context);
            alertDialog.setTitle(title);
            alertDialog.setMessage(warningMsg);
            alertDialog.setOnComfirmClick(onOkBtnClickListener);
            alertDialog.setOnCancelClick(onCancelBtnClickListener);
            alertDialog.show();
        } catch (Exception e) {
            Log.e("huyamin","e="+e.toString());
        }
    }


    /**
     * 显示选择性的dialog
     *
     * @param title
     * @param warningMsg 提示信息
     */
    @UiThread(propagation = UiThread.Propagation.REUSE)
    public void showDialog(Context context, String title, String warningMsg, String confirm, String cancel,
                           View.OnClickListener confirmClickListener, View.OnClickListener cancelClickListener, boolean canDismissByUser) {
        if (context instanceof Activity && ((Activity) context).isFinishing()) {
            return;
        }
        try {
            if (null != alertDialog) {
                if (alertDialog.getmMsg().getText().toString().equals(warningMsg) && alertDialog.isShowing())
                    return;
                alertDialog.dismiss();
                alertDialog = null;
            }

            alertDialog = new MaterialDialog(context);
            alertDialog.setTitle(title);
            alertDialog.setMessage(warningMsg);
            alertDialog.setOnComfirmClick(confirmClickListener);
            alertDialog.setOnCancelClick(cancelClickListener);
            alertDialog.setConfirm(confirm);
            alertDialog.setCancel(cancel);
            alertDialog.setmCanDismiss(canDismissByUser);
            alertDialog.show();
        } catch (Exception e) {
        }
    }


    @UiThread(propagation = UiThread.Propagation.REUSE)
    public void closeDialog() {
        if (null != alertDialog)
            alertDialog.dismiss();
        alertDialog = null;
    }

    public void tel(BaseActivity activity, String phone) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_DIAL);
        String tel = "tel:";
        phone.replace(" ", "");
        if (phone.contains("-")) {
            tel += phone.replace("-", ",");
        } else if (phone.contains("转")) {
            tel += phone.replace("转", ",");
        } else {
            tel += phone;
        }
        intent.setData(Uri.parse(tel));
        try {
            startActivityOnAnimation(activity, intent);
        } catch (Exception e) {
        }
    }


    public MaterialDialog getAlertDialog() {
        return alertDialog;
    }


}
